package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.BossModeDetailActivity;
import com.centling.smartSealForPhone.bean.BossModeInfo;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.view.CircleImageView;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * @author fionera
 */
public class BossModeListAdapter extends RecyclerView.Adapter<BossModeListAdapter.UnLockViewHolder> {
    private OnBossModeBeginListener beginListener;
    private OnBossModeEndListener endListener;
    private Context mContext;
    private List<BossModeInfo> mList;

    public BossModeListAdapter(Context context, List<BossModeInfo> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public BossModeListAdapter.UnLockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BossModeListAdapter.UnLockViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.rv_stamp_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final UnLockViewHolder myHolder, final int position) {
        BossModeInfo item = mList.get(position);

        ImageUtil.loadAvatarImage(item.getAvatar(), myHolder.civHead, R.drawable.iv_avatar_default);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        myHolder.tvTime.setText(simpleDateFormat.format(item.getCreatedDate()));
        myHolder.tvName.setText(item.getAppllicant());
        myHolder.tvTitle.setText("申请原因");
        myHolder.tvReason.setText(item.getApplyReason());
        if ("0".equals(item.getIsValid())) {
            String data = item.getStatus();
            switch (data) {
                case "0":
                    myHolder.tvStamp.setText("关闭");
                    myHolder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.blue2));
                    myHolder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
                    myHolder.tvStamp.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            endListener.onClick(mList.get(position));
                        }
                    });
                    break;
                case "1":
                    switch (item.getTimeStatus()) {
                        case "0":
                            myHolder.tvStamp.setText("等待开启");
                            myHolder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.change_seal_status_audit_success));
                            myHolder.tvStamp.setClickable(false);
                            myHolder.tvStamp.setBackground(null);
                            break;
                        case "1":
                            myHolder.tvStamp.setText("开启");
                            myHolder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.blue2));
                            myHolder.tvStamp.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
                            myHolder.tvStamp.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    beginListener.onClick(mList.get(position));
                                }
                            });
                            break;
                        default:
                            break;
                    }
                    break;
                case "2":
                    myHolder.tvStamp.setText("审核中");
                    myHolder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_auditing));
                    myHolder.tvStamp.setClickable(false);
                    myHolder.tvStamp.setBackground(null);
                    break;
                case "3":
                    myHolder.tvStamp.setText("审核失败");
                    myHolder.tvStamp.setTextColor(ContextCompat
                            .getColor(mContext, R.color.change_seal_status_audit_fail));
                    myHolder.tvStamp.setClickable(false);
                    myHolder.tvStamp.setBackground(null);
                    break;
                case "6":
                    myHolder.tvStamp.setText("已完成");
                    myHolder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_success));
                    myHolder.tvStamp.setClickable(false);
                    myHolder.tvStamp.setBackground(null);
                    break;
                default:
                    myHolder.tvStamp.setText("过期");
                    myHolder.tvStamp.setTextColor(
                            ContextCompat.getColor(mContext, R.color.change_seal_status_invalid));
                    myHolder.tvStamp.setClickable(false);
                    myHolder.tvStamp.setBackground(null);
                    break;
            }

        } else {
            myHolder.tvStamp.setText("换章失效");
            myHolder.tvStamp.setTextColor(ContextCompat.getColor(mContext, R.color.change_seal_status_audit_success));
            myHolder.tvStamp.setClickable(false);
            myHolder.tvStamp.setBackground(null);
        }
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, BossModeDetailActivity.class);
                String json = new Gson().toJson(mList.get(position));
                intent.putExtra("bossModeInfo", json);
                mContext.startActivity(intent);
            }
        });

        myHolder.tvStop.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class UnLockViewHolder
            extends RecyclerView.ViewHolder {
        CircleImageView civHead;
        TextView tvName, tvTime, tvReason, tvStamp, tvTitle, tvStop;

        UnLockViewHolder(View itemView) {
            super(itemView);
            civHead = itemView.findViewById(R.id.civ_stamp_list_preview);
            tvName = itemView.findViewById(R.id.tv_stamp_list_name);
            tvReason = itemView.findViewById(R.id.tv_stamp_list_reason);
            tvTime = itemView.findViewById(R.id.tv_stamp_list_time);
            tvTitle = itemView.findViewById(R.id.tv_stamp_list_reason_title);
            tvStamp = itemView.findViewById(R.id.tv_stamp_list_stamp);
            tvStop = itemView.findViewById(R.id.tv_stamp_list_stop);
        }
    }

    public interface OnBossModeBeginListener {
        void onClick(BossModeInfo item);
    }

    public void setOnBossModeBeginListener(OnBossModeBeginListener l) {
        this.beginListener = l;
    }

    public interface OnBossModeEndListener {
        void onClick(BossModeInfo item);
    }

    public void setOnBossModeEndListener(OnBossModeEndListener l) {
        this.endListener = l;
    }
}
